﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using Newtonsoft.Json;
using RabbitDownload.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace RabbitDownload.WebSite.Pixiv.PixivArt
{
    internal class ArtScanner
    {
        /// <summary>
        /// 下载一个任务列表
        /// </summary>
        /// <param name="ArticleIDList"></param>
        public void StartWithList(List<string> ArticleIDList)
        {
            int CompltedCount = 0;
            foreach (string Line in ArticleIDList)
            {
                new PrintLog().PercentLog(ArticleIDList.Count, ++CompltedCount);
                Start(Line);
            }
        }

        public static void Start(string Text)
        {
            int ArtId = new ArtHelper().GetArtId(Text);
            List<string> ImgList = new List<string>();
            Console.WriteLine(ArtId);
            if (ArtId == 0)
            {
                MessageHelper.Alert("画师ID不正确！");
                return;
            }
            int Pagecount = 1;
            string ArtName = "";
            while (true)
            {
                String API = string.Format(PixivStaticValue.ArtApi1, ArtId, Pagecount++);
                List<PixivArtJson.ImgInfo> ImginfoList = AnalysisApi(API);

                if (ImginfoList.Count > 0)

                {
                    ArtName = ImginfoList[0].ArtName;
                    ImgList.AddRange(GetAllPic(ImginfoList));
                    Console.WriteLine("已扫描{0}张图。", ImginfoList.Count);
                }
                else
                {
                    Console.WriteLine("所有任务完成！ 总数量{0}", ImgList.Count);
                    break;
                }
            }
            WebCurrency.SaveHelper.AutoSave(ImgList, $"P站画师【{ArtName}】PID：{ArtId}", "P站画师");
        }

        /// <summary>
        /// 解析API，并且添加进数组
        /// </summary>
        /// <param name="API"></param>
        /// <returns></returns>
        private static List<PixivArtJson.ImgInfo> AnalysisApi(string API)
        {
            Console.WriteLine(API);
            WebHeaderCollection webHeaderCollection = new WebHeaderCollection();

            webHeaderCollection.Add("Host", new Uri(API).Host);
            webHeaderCollection.Add("User-Agent", StaticValueOld.MoblieUserAgent);
            webHeaderCollection.Add("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            webHeaderCollection.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            webHeaderCollection.Add("Accept-Language", "zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            webHeaderCollection.Add("Pragma", "no-cache");
            webHeaderCollection.Add("Cache-Control", "no-cache");
            string Shtml = WebHelper.HttpGet(API, webHeaderCollection);

            Shtml = new PixivHelper().StandartJson(Shtml);
            List<String> JsonList = TextHelper.RegexTextList(Shtml, @"{[\w\W]*?illust_content_type[\w\W]*?}[\w\W]*?}");
            List<PixivArtJson.ImgInfo> Infolist = new List<PixivArtJson.ImgInfo>();
            if (JsonList.Count > 0)
            {
                foreach (String line in JsonList)
                {
                    try
                    {
                        PixivArtJson.Root PixivArtJson = JsonConvert.DeserializeObject<PixivArtJson.Root>(line);

                        Infolist.Add(new PixivArtJson.ImgInfo()
                        {
                            ArtID = PixivArtJson.illust_id,
                            ImgeTypeId = PixivArtJson.illust_ext,
                            ImgeCount = PixivArtJson.illust_page_count,
                            ImgeUrl = PixivArtJson.url,
                            ArtName = PixivArtJson.user_name,
                            ImgeId = PixivArtJson.illust_id
                        });

                        Console.WriteLine("该文章包含{0}张图。", Infolist.Count);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
            }
            return Infolist;
        }

        /// <summary>
        /// 根据得到的数据获取所有图片
        /// </summary>
        /// <param name="imginfoList"></param>
        /// <returns></returns>
        private static List<string> GetAllPic(List<PixivArtJson.ImgInfo> imginfoList)
        {
            List<string> AllImg = new List<string>();
            foreach (PixivArtJson.ImgInfo line in imginfoList)
            {
                if (PixivStaticValue.BlockComic)
                    AllImg.AddRange(new PixivHelper().CreateImgUrl(line.ImgeUrl, 1, line.ImgeId, line.ImgeTypeId).ToList<string>());
                else
                    AllImg.AddRange(new PixivHelper().CreateImgUrl(line.ImgeUrl, line.ImgeCount, line.ImgeId, line.ImgeTypeId).ToList<string>());
            }
            return AllImg;
        }
    }
}